package com.jiaai.cloud.wx.controller;

import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.wx.entity.TWxcomment;
import com.jiaai.cloud.wx.service.ITWxcommentService;
import com.jiaai.cloud.wx.service.ITWxshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-31
 */
@Controller
@RequestMapping("/api/v1/twxcomment")
public class TWxcommentController {
	@Autowired
    private ITWxcommentService itWxcommentService;

    //todo
    /**
     * 查询单个评论
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/querybyid/{id}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxcommentService.selectComUser(id));
        } catch (Exception e){
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo
    /**
     * 添加商店评论
     * @param userid
     * @param tWxcomment
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object add(@RequestHeader Integer userid
            , @RequestBody TWxcomment tWxcomment) throws ServiceException {
        tWxcomment.setUserid(userid);
        tWxcomment.setComdate(new Date());
        try {
            itWxcommentService.insert(tWxcomment);
            return RestResultGenerator.genSuccessResult("添加评论成功");
        } catch (Exception e){
            throw new ServiceException("添加评论失败", e.getMessage());
        }
    }

    //todo
    /**
     * 删除评论
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object delete(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itWxcommentService.deleteById(id);
            return RestResultGenerator.genSuccessResult("删除评论成功");
        } catch (Exception e){
            throw new ServiceException("删除评论失败", e.getMessage());
        }
    }

    //todo
    /**
     * 更改评论
     * @param tWxcomment
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object update(@RequestBody TWxcomment tWxcomment) throws ServiceException {
        try {
            itWxcommentService.updateById(tWxcomment);
            return RestResultGenerator.genSuccessResult("更改评论成功");
        } catch (Exception e){
            throw new ServiceException("更改评论失败", e.getMessage());
        }
    }

}
